<?php

namespace app\controllers;

use Yii;
use app\models\UserPoker;
use app\models\UserInfo;
use app\models\UserPokerSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * UserPokerController implements the CRUD actions for UserPoker model.
 */
class UserPokerController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        if( Yii::$app->user->isGuest){
            $this->redirect('?r=site/login');
        }
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all UserPoker models.
     * @return mixed
     */
    public function actionIndex()
    {



        $searchModel = new UserPokerSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        $dataProvider->setSort([
        'defaultOrder' => ['tts' => SORT_DESC],
   ]);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
           
        ]);
    }

    /**
     * Displays a single UserPoker model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new UserPoker model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new UserPoker();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->poker_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing UserPoker model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->poker_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing UserPoker model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }


    public function actionMyscore($id){

        return $this->render('my_score', [
            'model' => UserInfo::findOne($id),
        ]);
    }

    public function actionMyscoreAjax($id,$start_day,$end_day){
            $sql = "select poker_id,ui.user_id,up.user_agent_id,up.table_id,up.poker_name,up.score,up.in_score,up.out_score,up.ts,up.end_score,ui.name,uii.name as agent_name from user_poker up join  user_info ui on (up.user_id = ui.user_id) left join user_info uii on (up.user_agent_id = uii.user_id) where  up.user_id = ".$id." and up.ts >= '".$start_day."' and up.ts <= '".$end_day."' order by ts desc;";
            $rawData = Yii::$app->db->createCommand($sql)->queryAll();
            // echo $sql;
            echo json_encode($rawData);
    }


       public function actionMyagent($id){

        return $this->render('my_agent_data', [
            'model' => UserInfo::findOne($id),
        ]);
    }

    public function actionMyagentAjax($id,$start_day,$end_day){
            $sql = "select up.user_id, ui.name,uii.name as agent_name , up.user_agent_id,table_id,poker_name,sum(score) as score, sum(in_score) as in_score,sum(out_score) as out_score ,sum(end_score) as end_score   from   user_poker up , user_info ui, user_info uii where up.user_id = ui.user_id and up.user_agent_id = uii.user_id and  up.user_agent_id = ".$id." and up.ts >= '".$start_day."' and up.ts <= '".$end_day."' group by up.user_id order by score desc; ";
            $rawData = Yii::$app->db->createCommand($sql)->queryAll();
            
            echo json_encode($rawData);
    }

    public function actionMyagentdayAjax($id,$start_day,$end_day){


          $sql = "select up.user_id, date_format( up.ts,'%Y-%m-%d') as ts ,ui.name,uii.name as agent_name , up.user_agent_id,table_id,poker_name,sum(score) as score, sum(in_score) as in_score,sum(out_score) as out_score ,sum(end_score) as end_score   from   user_poker up , user_info ui, user_info uii where up.user_id = ui.user_id and up.user_agent_id = uii.user_id and  up.user_agent_id = ".$id." and up.ts >= '".$start_day."' and up.ts <= '".$end_day."' group by ts order by ts desc; ";
            $rawData = Yii::$app->db->createCommand($sql)->queryAll();
            
            echo json_encode($rawData);

    }

    /**
     * Finds the UserPoker model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return UserPoker the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = UserPoker::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
